package com.shutuo.callnummeiwei.utils;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.instwall.callnumlistener.CallNumNotification;
import com.shutuo.callnummeiwei.global.GlobalConstant;

import org.json.JSONObject;

import java.util.concurrent.ArrayBlockingQueue;

import ashy.earl.common.util.L;

public class MeiWeiJsParamsUtils {

    private static int callTable = -1;
    private static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(500, true);


    public static String  constructToJsParams(JSONObject jsonObject, int callNumType, boolean isCache ){
        String jsParams = null;
        if (!isCache) {
            if (callNumType == 0) {
                jsParams = constructJsParams(jsonObject);
            } else if (callNumType == 1) {
                jsParams = constructMeiWeiJsParams(jsonObject);
            }
        } else {
            jsParams = constructJsParamsForCache(jsonObject);
        }
        return jsParams;
    }


    /**
     * 构造调用JS时所要传递的参数
     *
     * @param jsonObject
     * @return
     */
    private static String constructJsParams(JSONObject jsonObject) {
        if (jsonObject == null) {
            return null;
        }
        int big = jsonObject.optInt("bigTable", 0);
        int small = jsonObject.optInt("smallTable", 0);
        int in = jsonObject.optInt("inTable", 0);

        int bigCTNumber = jsonObject.optInt("bigCTNumber", 0);
        int inCTNumber = jsonObject.optInt("inCTNumber", 0);
        int smallCTNumber = jsonObject.optInt("smallCTNumber", 0);

        int bigWaitTable = jsonObject.optInt("bigWaitTable", 0);
        int inWaitTable = jsonObject.optInt("inWaitTable", 0);
        int smallWaitTable = jsonObject.optInt("smallWaitTable", 0);


        big = big < 0 ? 0 : big;
        small = small < 0 ? 0 : small;
        in = in < 0 ? 0 : in;

        bigCTNumber = bigCTNumber < 0 ? 0 : bigCTNumber;
        inCTNumber = inCTNumber < 0 ? 0 : inCTNumber;
        smallCTNumber = smallCTNumber < 0 ? 0 : smallCTNumber;

        bigWaitTable = bigWaitTable < 0 ? 0 : bigWaitTable;
        inWaitTable = inWaitTable < 0 ? 0 : inWaitTable;
        smallWaitTable = smallWaitTable < 0 ? 0 : smallWaitTable;

//        return "window.PushDataForCommon(\"{\\\"bigTable\\\":" + big + ",\\\"smallTable\\\":" + small +
//                ",\\\"inTable\\\":" + in + ",\\\"bigCTNumber\\\":" + bigCTNumber +
//                ",\\\"inCTNumber\\\":" + inCTNumber + ",\\\"smallCTNumber\\\":" + smallCTNumber +
//                ",\\\"bigWaitTable\\\":" + bigWaitTable + ",\\\"inWaitTable\\\":" + inWaitTable +
//                ",\\\"smallWaitTable\\\":" + smallWaitTable + "}\")";
        String type = "meiwei";
        return "window.PushDataForCommon(\"{\\\"bigTable\\\":" + big + ",\\\"smallTable\\\":" + small +
                ",\\\"inTable\\\":" + in + ",\\\"bigCTNumber\\\":" + bigCTNumber +
                ",\\\"inCTNumber\\\":" + inCTNumber +   ",\\\"smallCTNumber\\\":" + smallCTNumber +
                ",\\\"bigWaitTable\\\":" + bigWaitTable + ",\\\"inWaitTable\\\":" + inWaitTable +
                ",\\\"factory\\\":" + "\\\"" + type+ "\\\""+
                ",\\\"smallWaitTable\\\":" + smallWaitTable + "}\")";
    }

    private static String constructMeiWeiJsParams(JSONObject jsonObject) {
        if (jsonObject == null) {
            return null;
        }
        String big = jsonObject.optString("bigTable", "0");
        String small = jsonObject.optString("smallTable", "0");
        String in = jsonObject.optString("inTable", "0");

        int bigWaitTable = jsonObject.optInt("bigWaitTable", 0);
        int inWaitTable = jsonObject.optInt("inWaitTable", 0);
        int smallWaitTable = jsonObject.optInt("smallWaitTable", 0);

        bigWaitTable = bigWaitTable < 0 ? 0 : bigWaitTable;
        inWaitTable = inWaitTable < 0 ? 0 : inWaitTable;
        smallWaitTable = smallWaitTable < 0 ? 0 : smallWaitTable;

//        return "window.PushDataForCommon(\"{\\\"bigTable\\\":" + big + ",\\\"smallTable\\\":" + small +
//                ",\\\"inTable\\\":" + in + ",\\\"bigCTNumber\\\":" + bigCTNumber +
//                ",\\\"inCTNumber\\\":" + inCTNumber + ",\\\"smallCTNumber\\\":" + smallCTNumber +
//                ",\\\"bigWaitTable\\\":" + bigWaitTable + ",\\\"inWaitTable\\\":" + inWaitTable +
//                ",\\\"smallWaitTable\\\":" + smallWaitTable + "}\")";
        String type = "meiwei";
        return "window.PushDataForCommon(\"{\\\"bigTable\\\":" + "\\\"" + big + "\\\"" + ",\\\"smallTable\\\":" + "\\\"" + small + "\\\"" +
                ",\\\"inTable\\\":" + "\\\"" + in + "\\\"" + ",\\\"bigCTNumber\\\":" + -1 +
                ",\\\"inCTNumber\\\":" + -1 + ",\\\"smallCTNumber\\\":" + -1 +
                ",\\\"bigWaitTable\\\":" + bigWaitTable + ",\\\"inWaitTable\\\":" + inWaitTable +
                ",\\\"factory\\\":" + "\\\"" + type+ "\\\""+
                ",\\\"smallWaitTable\\\":" + smallWaitTable + "}\")";
    }

    /**
     * 构造调用JS时所要传递的参数
     *
     * @param jsonObject
     * @return
     */
    private static String constructJsParamsForCache(JSONObject jsonObject) {
        if (jsonObject == null) {
            return null;
        }
        int waitTable = jsonObject.optInt("waitTable", 0);
        callTable = jsonObject.optInt("callTable", 0);


        waitTable = waitTable < 0 ? -1 : waitTable;
        callTable = callTable < 0 ? -1 : callTable;

        if (waitTable != -1) {
            queue.offer(waitTable);
        }

        if (callTable != -1) {
            if (!queue.contains(callTable)) {
                queue.remove(callTable);
            } else {
                queue.poll();
            }
        }
        return updateJsValueForCache(queue, callTable);
    }

    private static String updateJsValueForCache(ArrayBlockingQueue queue, int callTable) {
        if (callTable != -1) {
            handler.sendEmptyMessageDelayed(0, 15 * 1000);
        }
        return "window.PushDataForCache(\"{\\\"waitTable\\\":" + queue.toString() + ",\\\"callTable\\\":" +
                callTable + "}\")";
    }


    @SuppressLint("HandlerLeak")
    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            L.d(GlobalConstant.TAG, "%s",
                    "[CallNumberTemplatePlayListener] [Handler] [handleMessage]:" + msg.toString());
            if (msg.what == 0) {
                callTable = -1;
                String jsParams = updateJsValueForCache(queue, callTable);
                if (!TextUtils.isEmpty(jsParams)){
                    CallNumNotification.getCallNumNotification().notificationClient(jsParams);
                }
            }
        }
    };

}
